package cn.ylyhappy.systemwindow2

import android.content.Context
import android.graphics.PixelFormat
import android.os.Bundle
import android.view.Gravity
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.View.OnTouchListener
import android.view.ViewGroup
import android.view.WindowManager
import cn.ylyhappy.systemwindow2.databinding.FragmentFirstBinding

/**
 * A simple [Fragment] subclass as the default destination in the navigation.
 */
class FirstFragment : Fragment() {

    private var _binding: FragmentFirstBinding? = null

    // This property is only valid between onCreateView and
    // onDestroyView.
    private val binding get() = _binding!!

    private var mWindowManager: WindowManager? = null
    private var mLayoutParams: WindowManager.LayoutParams? = null
    private var mWindowViewLayout: DeskLayout? = null
    private var mOnTouchListener: OnTouchListener? = null

    private var x = 0.0f
    private var y = 0.0f
    private var mTouchStartX = 0.0f
    private var mTouchStartY = 0.0f
    private var top = 0.0f

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {

        _binding = FragmentFirstBinding.inflate(inflater, container, false)
        return binding.root

    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        createDeskLayout()
        createWindowManager()
        binding.buttonFirst.setText("显示系统窗口")
        binding.buttonFirst.setOnClickListener {
            showDesk()
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }


    fun showDesk() {
        mWindowManager?.addView(mWindowViewLayout!!, mLayoutParams!!)
        requireActivity().finish()
    }

    fun closeDesk() {
        mWindowManager?.removeView(mWindowViewLayout)
    }

    /**
     * 创建DeskLayout
     */
    fun createDeskLayout() {
        mWindowViewLayout = DeskLayout(requireActivity())
        mWindowViewLayout?.binding?.button?.setOnClickListener {
            closeDesk()
        }
        mOnTouchListener = OnTouchListener { v, event ->
            x = event.x
            y = event.y - 25
            when (event.action) {
                MotionEvent.ACTION_DOWN -> {
                    mTouchStartX = event.x
                    mTouchStartY = event.y
                }

                MotionEvent.ACTION_MOVE -> {
                    mLayoutParams?.x = (x - mTouchStartX).toInt()
                    mLayoutParams?.y = (y - mTouchStartY).toInt()
                    mWindowManager?.updateViewLayout(v, mLayoutParams)
                }

                MotionEvent.ACTION_UP -> {
                    mLayoutParams?.x = (x - mTouchStartX).toInt()
                    mLayoutParams?.y = (y - mTouchStartY).toInt()
                    mWindowManager?.updateViewLayout(v, mLayoutParams)

                    mTouchStartX = 0f
                    mTouchStartY = 0f
                    v.performClick()
                }
            }
            true
        }

        mWindowViewLayout?.setOnTouchListener(mOnTouchListener)
    }


    /**
     * 设置WindowManager
     */
    fun createWindowManager() {
        mWindowManager = requireContext().getSystemService(Context.WINDOW_SERVICE) as WindowManager
        mLayoutParams = WindowManager.LayoutParams()
        assert(mLayoutParams != null)
        mLayoutParams!!.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
        mLayoutParams!!.format = PixelFormat.RGBA_8888
        mLayoutParams!!.gravity = Gravity.TOP or Gravity.START
        mLayoutParams!!.width = WindowManager.LayoutParams.WRAP_CONTENT
        mLayoutParams!!.height = WindowManager.LayoutParams.WRAP_CONTENT
    }
}